package org.kzwl.service.app.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.category.CategoryBasic;
import org.kzwl.comm.pojo.dto.UserLoginDTO;
import org.kzwl.comm.pojo.dto.book.BookDTO;
import org.kzwl.comm.pojo.dto.category.CategoryDTO;
import org.kzwl.entity.app.Book;
import org.kzwl.entity.app.Category;
import org.kzwl.mapper.app.BookMapper;
import org.kzwl.mapper.app.CategoryMapper;
import org.kzwl.service.app.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-09-10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public ResponseData<List<Category>> queryAll() {
        return new ResponseData<>(this.baseMapper.selectList(null));
    }

    //region 基础查询
    @Override
    public List<CategoryDTO> queryBasic(CategoryBasic bo) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Category::getCuCategoryParent, bo.getCuCategoryParent());
        List<Category> list = categoryMapper.selectList(queryWrapper);
        List<CategoryDTO> categoryDTOS = JSONArray.parseArray(JSONObject.toJSONString(list), CategoryDTO.class);
        if (bo.getCuCategoryParent() > 0) {
            CategoryDTO dto = new CategoryDTO();
            dto.setCuCategoryId(bo.getCuCategoryParent());
            dto.setCuCategoryName("全部分类");
            categoryDTOS.add(0, dto);
//            List<Book> books = bookMapper.queryType(bo.getCuCategoryParent());
//            List<BookDTO> dtoList = JSONArray.parseArray(JSONObject.toJSONString(books), BookDTO.class);
//            dto.setAllList(dtoList);
        }
        return categoryDTOS;
    }
    //endregion

    public static void main(String[] args) {
        String a = "[{\"val\":\"黑色\",\"valueId\":1307651259356037120},{\"val\":\"白色\",\"valueId\":1307651293371842560},{\"val\":\"金色\",\"valueId\":1307654244752572416}]},{\"itemId\":2,\"name\":\"尺寸\",\"vals\":[{\"val\":\"SL\",\"valueId\":1307665082423066624},{\"val\":\"XL\",\"valueId\":1307695751085834240}]},{\"itemId\":3,\"name\":\"尺码\",\"vals\":[{\"val\":\"大号\",\"valueId\":1307695676280422400},{\"val\":\"小号\",\"valueId\":1307897885244932096}]}]";
        System.out.println(a.length());
    }
}
